
// 获取 APP 实例对象
const app = getApp()
Page({
  data:{
    fruits:[
      {id:'1',name:'苹果'},
      {id:'2',name:'香蕉'},
      {id:'3',name:'橘子'},
    ]
  },
  goFruitsDetail(e){
    let id = e.currentTarget.dataset.id
    let name = e.currentTarget.dataset.name 
    // url  支持绝对 和 相对路径

    // navigateTo
    // 保留当前页面，跳转到其他页面，不能跳转到tabbar页面
    // wx.navigateTo({
    //   url: `/pages/goods/detail/detail?id=${id}&name=${name}`,
    // })
    // wx.navigateTo({
    //   url: '/pages/index/index',
    // })
    // 卸载当前页面，跳转到其他页面，不能跳转到tabbar页面
    // wx.redirectTo({
    //   url: `./detail/detail?id=${id}&name=${name}`,
    // })
    // wx.redirectTo({
    //   url: '/pages/index/index',
    // })

    // 不能通过路由传参
    // wx.switchTab({
    //   url: `/pages/index/index?id=${id}`,
    // })

    wx.reLaunch({
      url: `/pages/index/index?id=${id}`,
    })
  },
  goIndex(){
    // 关闭所有非tabbar 页面，跳转到tabbar页面,不能跳转到非tabbar页面
    wx.switchTab({
      url: '/pages/index/index',
    })
   
  },
  goOtherMiniProgrom(){
    wx.navigateToMiniProgram({
      appId:'wxc97b21c63d084d92'
    })
  },
  onShow(){
    // 可以直接修改
    app.globalData.userInfo.name = '李四'
    console.log(app.globalData.userInfo.name);
  },
  saveData(){
    console.log('start....');
    // 异步存储
    wx.setStorage({
      key:'name',
      data:'李四',
      success(res){
        console.log(res);
      },
      fail(error){
        console.log(error);
      }
    })
    console.log("end....");
  },
  saveDataSync(){
    try {
      wx.setStorageSync('age', 18)
    } catch (error) {
      console.log(error);
    }
   
  },
  getData(){
    wx.getStorage({
      key:'name',
      success(res){
        console.log(res);
      },
      fail(error){
        console.log(error);
      }
    })
  },
  getDataSync(){
    // 返回值 
    try {
      let age = wx.getStorageSync('age')
      console.log("age==>",age);
    } catch (error) {
      console.log(error);
    }
    
  },
  removeData(){
    wx.removeStorage({
      key: 'name',
      success(res){
        console.log(res);
      },
      fail(error){
        console.log(error);
      }
    })
  },
  removeDataSync(){
    try {
      wx.removeStorageSync('age')
    } catch (error) {
      
    }
   
  },
  clearData(){
    try {
      wx.clearStorage()
    } catch (error) {
      
    }
    
  },
  clearDataSync(){
    try {
      wx.clearStorageSync()
    } catch (error) {
      
    }
   
  },
  getInfo(){
    wx.getStorageInfo({
      success(res){
        console.log(res);
      }
    })
  },
  getInfoSync(){
    let info = wx.getStorageInfoSync()
    console.log("info=>",info);
  }

})